package lolxxs.chenyanting.controller;


import lolxxs.chenyanting.entity.Result;
import lolxxs.chenyanting.entity.StatusCode;
import lolxxs.chenyanting.model.Statistics;
import lolxxs.chenyanting.service.IStatisticsService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tangwangzheng
 * @since 2021-12-30
 */
@RestController
@RequestMapping("/statistics")
public class StatisticsController {
    @Autowired
    @Qualifier(value = "statisticsServiceImpl")
    private IStatisticsService statisticsService;

    @PostMapping("/query")
    public Result query(HttpServletRequest request, @RequestBody Statistics statistics) {
        Claims claims = (Claims)request.getAttribute("claims");
        if (claims != null) {
            if (claims.getId() != null && (Integer)claims.get("identity") >=  1) {
                List<Statistics> query = statisticsService.query(statistics);
                if (query != null && query.size() > 0) {
                    return new Result(true, StatusCode.OK, "查询成功",query);
                } else {
                    return new Result(true, StatusCode.NOTFOUND, "没有找到记录");
                }
            }
        }
        return new Result(false, StatusCode.ACCESSERROR, "没有权限");
    }
}
